package com.haochang.chunkvod.controller.activity.cash;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.cash.CashData;
import com.haochang.chunkvod.model.cash.PrintEnum;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 *@author qintao
 *created at 2016/7/26 12:05
 * 设时预买
 */

public class SetTimePreBuyActivity extends BaseActivity implements KeyBoardDialog.KeyBoardOnSureListener
{

	private TitleView titleView;
	private BaseTextView tv_startTime;
	private BaseTextView tv_endTime;
	private BaseEditText et_hour, et_minute;
	private BaseEditText et_toHour, et_toMinute;
	private Button btn_preBuy, btn_sure;
	private String roomNum;
	private String roomTypeId;
	private String billId;
	private String billStatus;
	private int currentViewId = 0;
	private CashData cashData;
	private String startTime;
	private String endTime;
	private String longSetTime;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{
		roomNum = getIntent().getStringExtra(IntentKey.ROOM_NUM);
		roomTypeId = getIntent().getStringExtra(IntentKey.ROOM_TYPE_ID);
		billId = getIntent().getStringExtra(IntentKey.BILL_ID);
		billStatus = getIntent().getStringExtra(IntentKey.BILL_STATUS);
	}

	@Override
	public void initData()
	{
		cashData = new CashData(SetTimePreBuyActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_set_time_pre_buy);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(String.format("设时预买(%s)", roomNum)).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(SetTimePreBuyActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
		tv_startTime = (BaseTextView) findViewById(R.id.tv_startTime);
		tv_endTime = (BaseTextView) findViewById(R.id.tv_endTime);
		et_hour = (BaseEditText) findViewById(R.id.et_hour);
		et_minute = (BaseEditText) findViewById(R.id.et_minute);
		et_toHour = (BaseEditText) findViewById(R.id.et_toHour);
		et_toMinute = (BaseEditText) findViewById(R.id.et_toMinute);
		btn_preBuy = (Button) findViewById(R.id.btn_preBuy);
		btn_sure = (Button) findViewById(R.id.btn_sure);
		et_hour.setOnClickListener(mOnBaseClickListener);
		et_minute.setOnClickListener(mOnBaseClickListener);
		et_toHour.setOnClickListener(mOnBaseClickListener);
		et_toMinute.setOnClickListener(mOnBaseClickListener);
		btn_preBuy.setOnClickListener(mOnBaseClickListener);
		btn_sure.setOnClickListener(mOnBaseClickListener);
		cashData.requestDefaultTime(billId, roomTypeId, new CashData.ISetTimeListener()
		{
			@Override
			public void onRequestSuccess(JSONObject jsonObject)
			{
				if (jsonObject != null)
				{
					startTime = jsonObject.optString("begin_time");
					endTime = jsonObject.optString("end_time");
					longSetTime = jsonObject.optString("longest_time");
					if (!TextUtils.isEmpty(endTime))
					{
						tv_startTime.setText(String.format("已买到 %s", TimeUtils.getMinutesString(endTime)));
						if (TimeUtils.compareTime(longSetTime, endTime, 3 * 60 * 60 * 1000))
						{
							et_hour.setText("3");
							et_minute.setText("0");
						} else
						{
							setResultAfterTimeMinus(TimeUtils.timeMinus(longSetTime, endTime));
						}
						setResultAfterTimePlus(
								TimeUtils.timePlus(endTime, getIntValue(et_hour.getText().toString()), getIntValue(et_minute.getText().toString())));
					} else
					{
						tv_startTime.setText(String.format("计费开始 %s", TimeUtils.getMinutesString(startTime)));
						if (TimeUtils.compareTime(longSetTime, startTime, 3 * 60 * 60 * 1000))
						{
							et_hour.setText("3");
							et_minute.setText("0");
						} else
						{
							setResultAfterTimeMinus(TimeUtils.timeMinus(longSetTime, startTime));
						}
						setResultAfterTimePlus(
								TimeUtils.timePlus(startTime, getIntValue(et_hour.getText().toString()), getIntValue(et_minute.getText().toString())));
					}
					tv_endTime.setText(String.format("可设置到 %s", TimeUtils.getMinutesString(longSetTime)));
				}
			}
		});
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_preBuy:
				cashData.requestSubmitTime(billId, roomTypeId, getSetTime(), new CashData.ISetTimeListener()
				{
					@Override
					public void onRequestSuccess(JSONObject jsonObject)
					{
						Intent intent = new Intent(SetTimePreBuyActivity.this, CheckOutActivity.class);
						intent.putExtra(IntentKey.ROOM_NUM, roomNum);
						intent.putExtra(IntentKey.BILL_ID, billId);
						intent.putExtra(IntentKey.IS_FORCE_CHECKOUT, "0");
						intent.putExtra(IntentKey.CHECKOUT_TYPE, PrintEnum.PRINT_PRE_PAY);
						startActivity(intent);
					}
				});
				break;
			case R.id.btn_sure:
				cashData.requestSubmitTime(billId, roomTypeId, getSetTime(), new CashData.ISetTimeListener()
				{
					@Override
					public void onRequestSuccess(JSONObject jsonObject)
					{
						finish();
					}
				});
				break;
			case R.id.et_hour:
				currentViewId = R.id.et_hour;
				showKeyBoardDialog();
				break;
			case R.id.et_minute:
				currentViewId = R.id.et_minute;
				showKeyBoardDialog();
				break;
			case R.id.et_toHour:
				currentViewId = R.id.et_toHour;
				showKeyBoardDialog();
				break;
			case R.id.et_toMinute:
				currentViewId = R.id.et_toMinute;
				showKeyBoardDialog();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 自定义键盘输入
	 */
	private void showKeyBoardDialog()
	{
		KeyBoardDialog keyBoardDialog = new KeyBoardDialog(SetTimePreBuyActivity.this);
		keyBoardDialog.setKeyBoardOnSureListener(SetTimePreBuyActivity.this);
		keyBoardDialog.show();
	}

	@Override
	public void setValue(String value)
	{
		if (value.contains("."))
			ToastUtils.showText(SetTimePreBuyActivity.this, R.string.error_number);
		else if (value.length() > 9)
			ToastUtils.showText(SetTimePreBuyActivity.this, R.string.error_max_number);
		else
		{
			switch (currentViewId)
			{
			case R.id.et_hour:
				if (getIntValue(value) >= 0 && getIntValue(value) <= 23)
				{
					et_hour.setText(value);
				} else
				{
					et_hour.setText("23");
				}
				setResultAfterTimePlus(TimeUtils.timePlus(TextUtils.isEmpty(endTime) ? startTime : endTime, getIntValue(et_hour.getText().toString()),
						getIntValue(et_minute.getText().toString())));
				break;
			case R.id.et_minute:
				if (getIntValue(value) >= 0 && getIntValue(value) <= 59)
				{
					et_minute.setText(value);
				} else
				{
					et_minute.setText("59");
				}
				setResultAfterTimePlus(TimeUtils.timePlus(TextUtils.isEmpty(endTime) ? startTime : endTime, getIntValue(et_hour.getText().toString()),
						getIntValue(et_minute.getText().toString())));
				break;
			case R.id.et_toHour:
				if (getIntValue(value) >= 0 && getIntValue(value) <= 23)
				{
					et_toHour.setText(value);
				} else
				{
					et_toHour.setText("23");
				}
				setResultAfterTimeMinus(TimeUtils.timeMinus(TextUtils.isEmpty(endTime) ? startTime : endTime, getIntValue(et_toHour.getText().toString()),
						getIntValue(et_toMinute.getText().toString())));
				break;
			case R.id.et_toMinute:
				if (getIntValue(value) >= 0 && getIntValue(value) <= 59)
				{
					et_toMinute.setText(value.length() < 2 ? "0" + value : value);
				} else
				{
					et_toMinute.setText("59");
				}
				setResultAfterTimeMinus(TimeUtils.timeMinus(TextUtils.isEmpty(endTime) ? startTime : endTime, getIntValue(et_toHour.getText().toString()),
						getIntValue(et_toMinute.getText().toString())));
				break;
			default:
				break;
			}
		}
	}

	private int getIntValue(String value)
	{
		if (TextUtils.isEmpty(value))
			return 0;
		else
			return Integer.parseInt(value);
	}

	/**
	 * 两个时间相加后设置时间
	 * @param result
	 */
	private void setResultAfterTimePlus(String result)
	{
		if (!TextUtils.isEmpty(result))
		{
			String time[] = result.split(":");
			et_toHour.setText(Integer.parseInt(time[0]) > 24 ? Integer.parseInt(time[0]) - 24 + "" : time[0]);
			et_toMinute.setText(time[1].length() < 2 ? "0" + time[1] : time[1]);
		}
	}

	/**
	 * 两个时间相减后设置时间
	 * @param result
	 */
	private void setResultAfterTimeMinus(String result)
	{
		if (!TextUtils.isEmpty(result))
		{
			String time[] = result.split(":");
			et_hour.setText(time[0]);
			et_minute.setText(time[1]);
		}
	}

	/**
	 * 获取设置到的时间开始时间or结束时间+时长
	 */
	private String getSetTime()
	{
		String time = TextUtils.isEmpty(endTime) ? startTime : endTime;
		return TimeUtils.getNewTimeAfterTimePlus(time, getIntValue(et_hour.getText().toString()), getIntValue(et_minute.getText().toString()));
	}
}
